﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using ZQNB.BaseLib.Core;
using ZQNB.BaseLib.Core.Models;
using ZQNB.Common;
using ZQNB.Common.Attributes;
using ZQNB.Common.Ioc;
using ZQNB.Web.Common.Helpers;

namespace ZQNB.Web
{
    public class MetaConfig
    {
        public static void RegisterMetas()
        {
            NbMetaHelper helper = new NbMetaHelper();
            
            //初始化menu
            var menuMetas = helper.FindMenuMetas(true);
            var sideBarMenuService = CoreServiceProvider.LocateService<ISideBarMenuService>();
            var menus = menuMetas.Select(ConverToMenu).ToList();
            sideBarMenuService.Init(menus);

            //初始化op
            var opMetas = helper.FindOpMetas(true);
            var opService = CoreServiceProvider.LocateService<IOpService>();
            var ops = opMetas.Select(ConverToOp).ToList();
            opService.Init(ops);
        }

        private static SideBarMenu ConverToMenu(MenuAttribute from)
        {
            SideBarMenu to = new SideBarMenu();

            to.Href = from.Href;
            to.Text = from.Text;
            to.Class = from.Class;
            to.Position = from.Positon;
            to.Module = from.Module;
            to.SortNum = from.SortNum;
            to.Pk = from.Pk;
            to.ParentPk = from.ParentPk;

            return to;
        }
        
        private static Op ConverToOp(OpAttribute opAttribute)
        {
            return new Op()
            {
                Href = opAttribute.Href,
                Module = opAttribute.Module,
                Category = opAttribute.Category,
                Text = opAttribute.Text,
                SortNum = opAttribute.SortNum,
                ArgsName = opAttribute.ArgsName,
                DefaultRoles = opAttribute.DefaultRoles,
                CanEditDefaultRoles = opAttribute.CanEditDefaultRoles,
                MenuPks = opAttribute.MenuPks

            };
        }
    }


    public class WidgetMetaConfig
    {
        public static void Register()
        {
            var widgetMetaHelper = CoreServiceProvider.LocateService<WidgetMetaHelper>();
            List<WidgetMeta> widgetMetas = widgetMetaHelper.ReadWidgetMetas();
            widgetMetaHelper.SyncWidgetMetas(widgetMetas);
        }
    }

}